const mongoose = require('mongoose')

// 浏览历史模式定义
const historySchema = new mongoose.Schema({
  // 关联的用户
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  // 内容ID
  contentId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true
  },
  // 内容类型
  contentType: {
    type: String,
    required: true,
    enum: ['article', 'video', 'image', 'audio']
  },
  // 内容标题
  title: {
    type: String,
    required: true
  },
  // 内容缩略图
  thumbnail: {
    type: String,
    default: ''
  },
  // 浏览时间
  viewedAt: {
    type: Date,
    default: Date.now
  },
  // 停留时长（秒）
  duration: {
    type: Number,
    default: 0
  }
}, {
  timestamps: true
})

// 创建索引
historySchema.index({ user: 1, viewedAt: -1 })
historySchema.index({ user: 1, contentId: 1 })

// 创建并导出浏览历史模型
const History = mongoose.model('History', historySchema)
module.exports = History